package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starter.home.dao.UserDao;
import com.starter.home.domain.entity.User;
import com.starter.home.exception.UserException;
import com.starter.home.service.IUserService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Override
    public User toLogin(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        User user1 = baseMapper.selectOne(queryWrapper);
        if (Objects.isNull(user1)){
            throw new UserException("用户不存在！");
        }
        if (!user1.getStatus()){
            throw new UserException("你已经被锁定!请联系管理员");
        }
        if (!StringUtils.equals(user.getPwd(),user1.getPwd())){
            throw new UserException("密码不正确！");
        }
        return user1;

    }

    @Override
    public void changeStatus(Integer id, Boolean status) {
        User user = baseMapper.selectById(id);
        user.setStatus(status);
        int i = baseMapper.updateById(user);
        if (i <= 0){
            throw new UserException("删除用户异常，请稍后重试......");
        }
    }

    @Override
    public PageUtil getUserList(CommonParam commonParam) {
        QueryWrapper<User> queryWrapper = initQueryWrapper(commonParam);
        if (commonParam.getSize() == 0){
            //获取全部的数据
            List<User> list = this.baseMapper.selectList(queryWrapper);
            return new PageUtil(list.size(),0,list);
        }
        //分页查询货物详情
        Page<User> page = new Page<>(commonParam.getPage(),commonParam.getSize());
        queryWrapper.orderByAsc("id");
        Page<User> pageData = this.baseMapper.selectPage(page, queryWrapper);
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),pageData.getRecords());
    }

    @Override
    public void deleteById(Integer id) {
        int i = this.baseMapper.deleteById(id);
        if (i <= 0){
            throw new UserException("删除用户失败!");
        }
    }

    private QueryWrapper<User> initQueryWrapper(CommonParam commonParam) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(commonParam.getNickName())){
            queryWrapper.like("nick_name",commonParam.getNickName());
        }
        if (ObjectUtils.isNotEmpty(commonParam.getType())){
            queryWrapper.eq("type",commonParam.getType());
        }
        if (ObjectUtils.isNotEmpty(commonParam.getStatus())){
            queryWrapper.eq("status",commonParam.getStatus());
        }
        if (StringUtils.isNotBlank(commonParam.getSex())){
            queryWrapper.eq("sex",commonParam.getSex());
        }
        return queryWrapper;
    }
}
